package com.example.pro3_reactor;

import reactor.core.publisher.Flux;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;

/**
 * @program: reactivePro
 * @description: Schedulers.subscribeOn()  可以将上游的线程切换到其他线程上执行
 * @author: zy
 * @create: 2025-04-26 21:13
 */
public class App5_subscribeOn {
    public static void main(String[] args) {
        // 定义调度器
        Scheduler s = Schedulers.newParallel("scheduler", 4);
        final Flux< String > flux = Flux
                .range(1, 2)
                .map(i -> 10 + i)
                .subscribeOn(s)     // 全局操作切换线程, 无论设置的位置, 以第一个为准
                .map(i -> "value " + i);
        new Thread(() -> flux.subscribe(i -> System.out.println(Thread.currentThread().getName() + " -> " + i ))).start();


        //对比
//        Scheduler s = Schedulers.newParallel("scheduler", 4);
//        final Flux< String > flux = Flux
//                .range(1, 2)
//                .map(i -> 10 + i)
//                //.subscribeOn(s)     // 全局操作切换线程, 无论设置的位置, 以第一个为准
//                .map(i -> "value " + i);
//        flux.subscribe(i -> System.out.println(Thread.currentThread().getName() + " -> " + i ) );
    }
}
